/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ddnsserver;

import java.io.*;
import java.util.*;
/**
 *
 * @author Do Viet Trung
 */


public class NameServer {
    public enum TYPE_SERVER {
        PRIMARY,SECONDARY
    }

    private String HostName,
            Root,
            Serial,
            Refesh,
            Reply,
            Expire,
            Minimum,
            TTL;
    TYPE_SERVER Type;

    //contructor
    NameServer() {
        Type = TYPE_SERVER.PRIMARY;
        HostName = "";
        Root = "";
        Serial = "";
        Refesh = "";
        Reply = "";
        Expire = "";
        Minimum = "";
        TTL = "";
    }

    NameServer (
            TYPE_SERVER type,
            String hostName,
            String root,
            String serial,
            String refesh, 
            String reply,
            String expire,
            String minimum,
            String ttl) {
        Type = type;
        HostName = hostName;
        Root = root;
        Serial = serial;
        Refesh = refesh;
        Reply = reply;
        Expire = expire;
        Minimum = minimum;
        TTL = ttl;
    }

    NameServer (TYPE_SERVER type, String file) throws Exception {
        if(!file.isEmpty()) {
            Type = type;
            BufferedReader br = new BufferedReader(new FileReader(file));
            {
                String txt;
                Vector<Vector<String>> fields = new Vector<Vector<String>>();
                Vector<String> field;
                
                while (true) {
                    txt = br.readLine();

                    if(txt == null) {
                        break;
                    }

                    field = JProcess.parseString(txt, ' ');
                    fields.add(field);
                    int i = field.size();
                }

                addData(fields);
            }
        }
    }
    //End Contructure

    //Property
    public String getHostName() {
        return HostName;
    }

    public String getRoot() {
        return Root;
    }

    public String getSerial() {
        return Serial;
    }

    public String getRefeshTime() {
        return Refesh;
    }

    public String getReplyTime() {
        return Reply;
    }

    public String getExpireTime() {
        return Expire;
    }

    public String getMinimumTime() {
        return Minimum;
    }

    public String getTTL() {
        return TTL;
    }

    public TYPE_SERVER getTypeServer() {
        return Type;
    }

    public void setHostName(String hostName) {
        if(!hostName.isEmpty()) {
            HostName = hostName;
        }
    }
     
    public void setRoot(String root) {
        if(!root.isEmpty()) {
            Root = root;
        }
    }

    public void setSerialTime(String serialTime) {
        if(!serialTime.isEmpty()) {
            Serial = serialTime;
        }
    }

    public void setRefeshTime (String refeshTime) {
        if(!refeshTime.isEmpty()) {
            Refesh = refeshTime;
        }
    }
    
    public void setReplyTime(String replyTime) {
        if(!replyTime.isEmpty()) {
            Reply = replyTime;
        }
    }

    public void setMinimumTime(String minimumTime) {
        if(!minimumTime.isEmpty()) {
            Minimum = minimumTime;
        }
    }

    public void setExpireTime (String expireTime) {
        if (!expireTime.isEmpty()) {
            Expire = expireTime;
        }
    }

    public void setTTL(String ttl) {
        if (!ttl.isEmpty()) {
            TTL = ttl;
        }
    }
    
    public void setTypeServer(TYPE_SERVER type) {
        Type = type;
    }
    //end Property

    //Method
    private void addData (Vector<Vector<String>> fields) {
        if (fields.size() < 6) {
            return;
        }

        TTL = fields.elementAt(0).elementAt(1);
        HostName = fields.elementAt(1).elementAt(1);
        Root = fields.elementAt(1).elementAt(2);
        Serial = fields.elementAt(2).elementAt(0);
        Refesh = fields.elementAt(3).elementAt(0);
        Reply = fields.elementAt(4).elementAt(0);
        Expire = fields.elementAt(5).elementAt(0);
        Minimum = fields.elementAt(6).elementAt(0);
    }
    //end Method
}
